package com.cndw.kungfu.model.task;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Task;
import com.cndw.kungfu.domain.TaskMode;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;

/**
 * 玩家的任务集
 * 
 * @author fantadust
 * @date 2011-4-25 下午08:45:15
 */
public class PlayerTasks {
	/**
	 * 用户id
	 */
	private long playerId;
	/**
	 * 已经完成的任务
	 */
	private Map<Integer, Integer> endTaskIds;
	/**
	 * 正在做的任务
	 */
	private Map<Integer, Task> doingTasks;
	/**
	 * 当前等级可操作的全部任务
	 */
	private Map<Integer, TaskMode> currentTaskModes;
	/**
	 * 任务完成进展
	 */
	private Map<Integer, TaskProgress> taskProgress = new HashMap<Integer, TaskProgress>();
	/**
	 * 改变状态的任务id
	 */
	public Set<Integer> changeIds = new HashSet<Integer>();
	/**
	 * 监控的道具id
	 */
	private Map<Integer, Set<Integer>> overseeIds = new HashMap<Integer, Set<Integer>>();
	/**
	 * 特殊任务归类 轮回任务
	 */
	private Map<Integer, List<Integer>> specialTaskIds = new HashMap<Integer, List<Integer>>();

	private int awaitSecretId;

	public PlayerTasks(long playerId) {
		this.playerId = playerId;
	}

	public void setEndTaskIds(Map<Integer, Integer> endTaskIds) {
		this.endTaskIds = endTaskIds;
	}

	public void setDoingTasks(Map<Integer, Task> doingTasks) {
		this.doingTasks = doingTasks;
	}

	public void setTaskProgress(Map<Integer, TaskProgress> taskProgress) {
		if (taskProgress != null) {
			this.taskProgress = taskProgress;
		}
	}

	/**
	 * 任务初始化
	 */
	public void initFirst() {
		refreshTaskModes();
	}

	public TaskProgress getProgressByTaskId(int taskId) {
		if (taskProgress.containsKey(taskId)) {
			return taskProgress.get(taskId);
		} else {
			TaskProgress e = new TaskProgress();
			taskProgress.put(taskId, e);
			return e;
		}

	}

	/**
	 * 是否有这个任务,已接或者可接
	 * 
	 * @param taskId
	 * @return
	 */
	public boolean currentCanDo(int taskId) {
		return currentTaskModes.containsKey(taskId);
	}

	public Set<Integer> currentCanDoIds() {
		return currentTaskModes.keySet();
	}

	public boolean isDoing(int taskId) {
		return doingTasks.containsKey(taskId);
	}

	public Task geTask(int taskId) {
		return doingTasks.get(taskId);
	}

	public Collection<Task> geTasks() {
		return doingTasks.values();
	}

	/**
	 * 获取特别的任务id
	 * 
	 * @param type
	 * @return
	 */
	public List<Integer> getSpecialIds(int npcId) {
		return specialTaskIds.get(npcId);
	}

	/**
	 * 刷新任务,还得解决某些未知问题
	 */
	public void refreshTaskModes() {
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		Collection<TaskMode> taskModes = Context.getCacheCenter().getNpcTasks();
		Map<Integer, TaskMode> taskModesTrue = new HashMap<Integer, TaskMode>();
		int morningTime = TimeProcesser.getMorningTime();
		specialTaskIds.clear();// 清理我的轮回任务列表

		int dartLeaveTime = 0;
		if (player.getLevel() >= 20) {
			dartLeaveTime = Context.getTaskServer().getTaskLaveTimes(playerId, TaskMode.typeDart);
		}

		for (TaskMode e : taskModes) {
			int taskId = e.getTaskId();
			if (e.isTypeIntent()) {// 目标任务跳过
				continue;
			}
			if (doingTasks.containsKey(taskId)) {// 任务已接到

			} else if (e.isTypeDart() && dartLeaveTime <= 0) {// 押镖任务不够次数不显示
				continue;
			} else if (e.getLevel() > player.getLevel()) {// 等级不够
				continue;
			} else if (e.getLevelMax() > 0 && e.getLevelMax() < player.getLevel()) {// 等级超过
				continue;
			} else if (e.getFrontTaskId() > 0 && !endTaskIds.containsKey(e.getFrontTaskId())) {
				continue;// 未完成前置任务
			} else if (endTaskIds.containsKey(taskId)) {
				int finishTime = endTaskIds.get(taskId);
				if (e.getTimesLimit() == 0 || (e.getTimesLimit() == 1 && finishTime >= morningTime)) {// 一日一次的任务
					if (taskProgress.containsKey(taskId)) {// 容错 刪除已經完成的任務id的進度
															// 不是最好的办法
						taskProgress.remove(taskId);
					}
					continue;
				}
			}

			if (e.isSpecialType() && !e.isTypeDart()) {
				int specialId = e.getSourceNpcId();
				if (specialTaskIds.containsKey(specialId) == false) {
					specialTaskIds.put(specialId, new ArrayList<Integer>());
				}
				specialTaskIds.get(specialId).add(taskId);

				if (doingTasks.containsKey(taskId) == false) {
					continue;
				}
			}

			taskModesTrue.put(taskId, e);
			if (currentTaskModes != null && !currentTaskModes.containsKey(taskId)) {
				changeIds.add(taskId);
			}
		}
		currentTaskModes = taskModesTrue;
	}

	/**
	 * 清理异常情况导致的任务进度表
	 * 
	 * @return
	 */
	public int gcTaskProgress() {
		int delNum = 0;
		try {
			if (!taskProgress.isEmpty()) {
				Iterator<Integer> it = taskProgress.keySet().iterator();
				while (it.hasNext()) {
					Integer taskId = (Integer) it.next();
					if (!doingTasks.containsKey(taskId)) {
						it.remove();
						delNum++;
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return delNum;
	}

	public void acceptTask(Task task) {
		doingTasks.put(task.getTaskId(), task);
		changeIds.add(task.getTaskId());
	}

	public void submitTask(Task task) {
		doingTasks.remove(task.getTaskId());
		endTaskIds.put(task.getTaskId(), task.getFinishTime());
		taskProgress.remove(task.getTaskId());
		currentTaskModes.remove(task.getTaskId());
		refreshTaskModes();
	}
	//完美清理监视列表
	@Deprecated
	public void gcOversee(Task task) {
		try {
			TaskMode taskMode = Context.getTaskServer().geTaskMode(task.getTaskId());
			if (!overseeIds.isEmpty() && taskMode.getTaskConditionsList() != null && taskMode.getTaskConditionsList().size() >= 1) {
				for (TaskCondition e : taskMode.getTaskConditionsList()){
					if (overseeIds.containsKey(e.getTargerId())) {
						Set<Integer> taskIds = overseeIds.get(e.getTargerId());
						if (taskIds.contains(taskMode.getTaskId())) {					
							if (taskIds.size() == 1) {
								overseeIds.remove(e.getTargerId());
							}else {
								taskIds.remove(taskMode.getTaskId());
							}						
						}
					}		
				}				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 删除任务的一些数据清理
	 * 
	 * @param task
	 */
	public void cancelTask(Task task) {
		doingTasks.remove(task.getTaskId());
		changeIds.remove(task.getTaskId());
		taskProgress.remove(task.getTaskId());
		currentTaskModes.remove(task.getTaskId());
	}

	/**
	 * 收集物品和功能触发需求一起监控.保证id不冲突
	 * 
	 * @param signId
	 * @param taskId
	 */
	public void overseeAdd(int signId, int taskId) {
		if (!overseeIds.containsKey(signId)) {
			overseeIds.put(signId, new HashSet<Integer>());
		}
		Set<Integer> taskIds = overseeIds.get(signId);
		taskIds.add(taskId);
	}

	public Set<Integer> overseeGet(int signId) {
		return overseeIds.get(signId);
	}
	
	public boolean overseeConstins(int signId){
		return overseeIds.containsKey(signId);
	}

	public void overseeRemove(int signId) {
		overseeIds.remove(signId);
	}

	/**
	 * 表示曾经完成过任务
	 * 
	 * @param taskId
	 * @return
	 */
	public boolean checkDoneOnce(int taskId) {
		return endTaskIds.containsKey(taskId);
	}

	public int getAwaitSecretId() {
		return awaitSecretId;
	}

	public void setAwaitSecretId(int awaitSecretId) {
		this.awaitSecretId = awaitSecretId;
	}

}
